<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HashMap 扰动函数深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
            background-size: 50px 50px;
            animation: drift 20s linear infinite;
        }
        
        @keyframes drift {
            from { transform: translate(0, 0); }
            to { transform: translate(50px, 50px); }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(to bottom, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 24px;
            overflow-x: auto;
            position: relative;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 20px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::after {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 10%, transparent 10%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.3; }
        }
        
        .feature-card {
            background: white;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 1px solid rgba(0, 0, 0, 0.05);
        }
        
        .feature-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    HashMap 扰动函数
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度解析</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索 Java HashMap 中的哈希优化艺术，理解高低位异或运算背后的设计智慧
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-code text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">优雅实现</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-chart-line text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">性能优化</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-3xl mb-2"></i>
                        <p class="text-sm opacity-80">冲突降低</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-16 max-w-6xl">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Java 的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">HashMap</code> 实现中，计算键的哈希值时，不是直接使用键的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">hashCode()</code> 值，而是对其进行了进一步的处理，即将高 16 位与低 16 位做异或运算。这种操作被称为<strong class="text-purple-700">扰动函数（hash spreading or hash perturbation function）</strong>。
            </p>
        </div>

        <!-- Visual Diagram -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-center text-gray-800">扰动函数工作原理</h3>
            <div class="mermaid">
                graph LR
                    A[原始 hashCode] --> B[高16位]
                    A --> C[低16位]
                    B --> D[右移16位]
                    D --> E[异或运算]
                    C --> E
                    E --> F[最终哈希值]
                    
                    style A fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style F fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#333,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Feature Cards -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-random text-white text-2xl"></i>
                </div>
                <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">降低哈希冲突的概率</h3>
                <p class="text-gray-600 leading-relaxed mb-4">
                    哈希表的理想情况是将元素均匀地分布到不同的桶中，但实际应用中，键的 <code class="bg-gray-100 text-gray-800 px-2 py-1 rounded text-sm">hashCode()</code> 值可能分布不均匀，尤其是在哈希函数的高位和低位具有不同的分布特征时。
                </p>
                <p class="text-gray-600 leading-relaxed">
                    通过对高 16 位和低 16 位做异或运算，能够将高位的变化引入到低位。这使得哈希值的低位更加均匀，减少了哈希冲突，提升了 HashMap 的性能。
                </p>
            </div>

            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-chart-bar text-white text-2xl"></i>
                </div>
                <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">提高散列质量</h3>
                <p class="text-gray-600 leading-relaxed">
                    将高位与低位进行异或运算是一种简单但有效的手段，能够将高位的信息"混合"到低位。这种"扰动"提高了最终哈希值的散列质量，使得键更均匀地分布到哈希表的各个桶中，从而提高查找、插入和删除操作的效率。
                </p>
            </div>
        </div>

        <!-- Highlight Box -->
        <div class="highlight-box">
            <h4 class="text-2xl font-bold mb-3 flex items-center">
                <i class="fas fa-lightbulb mr-3"></i>
                核心洞察
            </h4>
            <p class="text-lg leading-relaxed relative z-10">
                在计算桶索引时，HashMap 使用的是哈希值的低位部分（通过 <code class="bg-white bg-opacity-20 px-2 py-1 rounded">hash & (table.length - 1)</code> 计算索引）。如果直接使用原始的 hashCode() 值，低位分布不均匀可能导致哈希冲突增多，从而降低 HashMap 的性能。
            </p>
        </div>

        <!-- Additional Features -->
        <div class="grid md:grid-cols-2 gap-8 my-12">
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-balance-scale text-white text-2xl"></i>
                </div>
                <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">兼容性和性能的折中</h3>
                <p class="text-gray-600 leading-relaxed">
                    HashMap 设计时要在性能和散列质量之间找到平衡。异或高 16 位和低 16 位的操作能够有效提高散列质量，同时计算成本非常低，不会显著影响性能。
                </p>
            </div>

            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-expand-arrows-alt text-white text-2xl"></i>
                </div>
                <h3 class="section-title text-2xl font-bold mb-4 text-gray-800">应对哈希表大小变化</h3>
                <p class="text-gray-600 leading-relaxed">
                    HashMap 的大小通常是 2 的幂次方，通过混合高位和低位，能够更好地适应不同大小的哈希表，避免由于哈希表扩容或缩容导致的哈希冲突问题。
                </p>
            </div>
        </div>

        <!-- Code Example -->
        <div class="content-card rounded-2xl p-10">
            <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">代码示例</h3>
            <p class="text-gray-600 mb-6">在 HashMap 中的扰动函数实现如下：</p>
            <div class="code-block">
                <pre><code>static final int hash(Object key) {
    int h;
    // 将 hashCode 的高 16 位与低 16 位进行异或运算
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}</code></pre>
            </div>
        </div>

        <!-- Summary Visualization -->
        <div class="content-card rounded-2xl p-10 mt-12">
            <h3 class="text-2xl font